package com.example.demo.controller;


import com.example.demo.Service.MessageService;
import com.example.demo.mapper.MessageMapper;
import com.example.demo.model.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/message")
@RestController
public class MessageController {

    @Autowired
    MessageService messageService;

//    private List<Message> messageList = new ArrayList<>();

    @RequestMapping(value = "/publish", method = RequestMethod.POST)
    public String publish(@RequestBody Message message){

        if (!StringUtils.hasLength(message.getFrom()) ||
             !StringUtils.hasLength(message.getTo()) ||
                !StringUtils.hasLength(message.getMessage()) ){
            return "{\"ok\": 0}";
        }

        messageService.addMessage(message);
        return "{\"ok\": 1}";

    }

    @RequestMapping("/getList")
    public List<Message> getList(){
        return messageService.selectAllMessage();
    }



}
